package com.gilight.base.service.impl;

import com.gilight.base.base.BusinessException;
import com.gilight.base.common.BzdmConstant;
import com.gilight.base.common.Constant;
import com.gilight.base.common.RoleStatus;
import com.gilight.base.dao.SysPermissionMapper;
import com.gilight.base.model.SysPermission;
import com.gilight.base.service.SysPermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;


    @Override
    @CacheEvict(value = BzdmConstant.REDIS_LOADMENU, allEntries = true)
    public void add(SysPermission sysPermission) {
        if (StringUtils.isEmpty(sysPermission.getName())) {
            throw new BusinessException("菜单名不能为空");
        }
        sysPermission.setStatus(RoleStatus.ENABLE);
        sysPermissionMapper.insertSelective(sysPermission);
    }

    @Override
    @CacheEvict(value = BzdmConstant.REDIS_LOADMENU, allEntries = true)
    public void updateStatus(Long id, Integer status) {
        SysPermission sysPermission = new SysPermission(id,status);
        sysPermissionMapper.updateByPrimaryKeySelective(sysPermission);
    }

    @Override
    @CacheEvict(value = BzdmConstant.REDIS_LOADMENU, allEntries = true)
    public void edit(SysPermission sysPermission) {
        SysPermission old = sysPermissionMapper.selectByPrimaryKey(sysPermission.getId());
        if (StringUtils.isEmpty(sysPermission.getName())) {
            throw new BusinessException("菜单名不能为空");
        }
        old.setName(sysPermission.getName());
        old.setIcon(sysPermission.getIcon());
        old.setOrderSeq(sysPermission.getOrderSeq());
        old.setRoute(sysPermission.getRoute());
        old.setUrl(sysPermission.getUrl());
//        old.setAppKey(sysPermission.getAppKey());
        sysPermissionMapper.updateByPrimaryKey(old);
    }

    public List<SysPermission> selectByParentId(Long parentId) {
        return sysPermissionMapper.selectByParentId(parentId);
    }

    @Override
    public List<SysPermission> getPermissionButton(Long id) {
        return sysPermissionMapper.selectButtonByParentId(id, Constant.BUTTON);
    }

    @Override
    public List<SysPermission> getPermissionButtonByRoute(String route) {
        return sysPermissionMapper.selectPermissionButtonByRoute(route);
    }

    @Override
    public List<Map<String,Object>> getRolePermissionButton(Long id, Long roleId) {
        return sysPermissionMapper.selectButtonByParentIdandRoleId(id,roleId,Constant.BUTTON);
    }
    @Override
    public SysPermission selectByRoute(String route){
        SysPermission sysPermission = new SysPermission();
        if(StringUtils.isNotEmpty(route)){
            return sysPermissionMapper.selectByRoute(route);
        }
        return sysPermission;
    }

    /**
     * 根据用户名、菜单ID获取该用户分配的菜单的数据规则ID
     * @param userId 用户ID
     * @param permissionId 菜单ID
     * @return
     */
    @Override
    public String queryUserDataRuleIds(String userId,int permissionId){
        return sysPermissionMapper.queryUserDataRuleIds(userId,permissionId);
    }
}
